<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Aauth extends Controller {

	public function action_index() {
		if ($this->request->post('login') || $this->request->post('pass')) {
			$userObj = ORM::factory('Aadmins', array(
	            'login' => $this->request->post('login'),
	            'pass' => md5($this->request->post('pass')),
	        ));
	        echo $userObj->login;

	        if (empty($userObj->id)) {
	        	$error = TRUE;
	        }
	        else {
	        	Cookie::set('admin', $userObj->id);
	        	Cookie::set('role', $userObj->role);
	        	setcookie('elfinder1903485', 1, time() + 3600, '/');
	        	$this->redirect('administration');
	        }
		}
		else {
			$error = FALSE;
		}

        $this->response->body(View::factory('alogin', array(
        	'error' => $error,
        )));
	}

	public function action_logout() {
		Cookie::delete('admin');
		Cookie::delete('role');
		unset($_COOKIE['elfinder1903485']);
		$this->redirect('administration/aauth');
	}
}
